package com.bdqn.tour.servlet.comments;



import com.bdqn.tour.dao.HotelImplementationClass;
import com.bdqn.tour.dao.impl.ImplUersDao;
import com.bdqn.tour.entity.Comment;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/er")
public class TexthamServlet extends HttpServlet {
    private static final int RECORDS_PER_PAGE = 4; // 每页显示的记录数

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/plain;charset=utf-8");
        int currentPage = 1;
        System.out.println("aa");
        if (request.getParameter("currentNo") != null) {
            currentPage = Integer.parseInt(request.getParameter("currentNo"));
            System.out.println(currentPage);
        }

        int start = (currentPage - 1) * RECORDS_PER_PAGE;
        int totalRecords = getTotalRecords(); // 获取总记录数
        int totalPages = (int) Math.ceil((double) totalRecords / RECORDS_PER_PAGE);

        List<Comment> commentsList = getComments(start, RECORDS_PER_PAGE); // 获取当前页的评论数据

        request.setAttribute("commentsList", commentsList);
        request.setAttribute("currentNo", currentPage);
        request.setAttribute("totalPages", totalPages);

        request.getRequestDispatcher("/backpinglunManagement.jsp").forward(request, response);
    }

    private int getTotalRecords() {
        HotelImplementationClass commentDao = new ImplUersDao();
        return commentDao.getTotalCommentsCount1();
        // 从数据库中获取总记录数
        // 示例：return commentDao.getTotalRecords();
    }

    private List<Comment> getComments(int start, int limit) {
        HotelImplementationClass commentDao = new ImplUersDao();
        return commentDao.getComments(start, limit);

        // 从数据库中获取指定范围的评论数据
        // 示例：return commentDao.getComments(start, limit);
        // 返回当前页的评论数据
    }
}
